/*
 * 文件名：SubpackCotController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月17日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.SubpackSuppleDomain;
import top.infopub.mgr.bpa.jsonVo.SubpackCotJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.SubpackSuppleService;
import top.infopub.mgr.bpa.vo.SubpackSuppleVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.bpa.SubpackCot;
import top.infopub.model.bpa.SubpackSupple;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


/**对内合同
 * @author wuzhiyang
 * @version 2020年2月17日
 * @see SubpackSuppleController
 * @since商务管理/对内合同/分包合同/补充协议
 */
@Controller
@RequestMapping("/subpackSupple")
public class SubpackSuppleController extends GenericFlowController {

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private SubpackSuppleService subpackSuppleService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    /**
     * 分包合同补充协议列表
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<SubpackSuppleDomain> queryDataByCondition(SubpackSuppleVo subpackSuppleVo,
                                                           ParamPage paramPage) throws ParseException {
        Page<SubpackSuppleDomain> page = new Page<SubpackSuppleDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (subpackSuppleVo.getEndDate() != null) {
            subpackSuppleVo.setEndDate(DateUtils.addDays(subpackSuppleVo.getEndDate(), 1));
        }
        page = subpackSuppleService.selectByDomainWhere(page, subpackSuppleVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     *@Description:  从分包合同评审获取项目下拉
     *@Param:
     *@return:
     *@Author: Tszzzzzzz
     *@date: 2021/6/17
     */
    @RequestMapping("/queryProject")
    @ResponseBody
    public RspVo queryProject(){
        return subpackSuppleService.selectProject();
    }


    /**
     *@Description: 详情
     *@Param:
     *@return:
     *@Author: Tszzzzzzz
     *@date: 2021/6/23
     */
    @RequestMapping("/querySuppleDetail")
    @ResponseBody
    public RspVo querySuppleDetail (String id){
        return subpackSuppleService.queryPackDetail(id);
    }

    /**
     *@Description: 保存/修改
     *@Param:
     *@return:
     *@Author: Tszzzzzzz
     *@date: 2021/6/23
     */
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save (String bodyStr,Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        SubpackCotJsonVo vo = JSON.parseObject(s,SubpackCotJsonVo.class);
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(vo.getFileAtt()!=null){
            String[] fileAtt = vo.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(userDtl,vo.getSubpackCot().getProCode(),file);
                attachmentList.add(attachment);
            }
        }
        return subpackSuppleService.saveAll(bodyStr,submitType,userDtl,vo.getSubpackSupple(),vo.getInnerListings(),vo.getCompPlan(),attachmentList);
    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 分包合同id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
        SubpackSupple subpackSupple = subpackSuppleService.selectBySguid(id);
        request.setAttribute("isAuditFlag", "1");
        if (subpackSupple!=null){
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), subpackSupple.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(subpackSupple.getProcessId()) && subpackSupple.getProcessId().length() > 10){
                request.setAttribute("processId", subpackSupple.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(subpackSupple.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), subpackSupple.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return new ModelAndView("bpa/lay-SubSupplementaryAgreement-detail");
    }
    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //审批
        bpaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }
}
